<template>
  <el-form inline="true">
    <el-form-item label="竞赛日期区间">
      <el-date-picker
        v-model="datetimeRange"
        style="width: 500px"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
    </el-form-item>
    <el-form-item label="竞赛名称">
      <el-input v-model="params.title" placeholder="请您输入要搜索的竞赛名称" />
    </el-form-item>
    <el-form-item>
      <el-button @click="onSearch" plain>搜索</el-button>
      <el-button @click="onReset" plain type="info">重置</el-button>
      <el-button type="primary" :icon="Plus" plain @click="onAddExam"
        >添加竞赛</el-button
      >
    </el-form-item>
  </el-form>
  <!-- 表格 -->
  <el-table height="526px" :data="examList">
    <el-table-column prop="title" width="180px" label="竞赛标题" />
    <el-table-column prop="startTime"  label="竞赛开始时间" />
    <el-table-column prop="endTime"  label="竞赛结束时间" />
    <el-table-column label="是否开赛" width="100px">
      <template #default="{ row }">
        <div v-if="isExamStatus(row) == 1">
          <el-tag type="warning">已开赛</el-tag>
        </div>
        <div v-else-if="isExamStatus(row) == 2">
          <el-tag type="info">未开赛</el-tag>
        </div>
        <div v-else-if="isExamStatus(row) == 3">
          <el-tag type="info">已完赛</el-tag>
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="status" width="100px" label="是否发布">
      <template #default="{ row }">
        <div v-if="row.status == 0">
          <el-tag type="danger">未发布</el-tag>
        </div>
        <div v-if="row.status == 1">
          <el-tag type="success">已发布</el-tag>
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="createName" width="140px" label="创建用户" />
    <el-table-column prop="createTime" width="180px" label="创建时间" />
    <el-table-column label="操作" width="180px">
      <template #default="{ row }">
        <!-- 未开赛  已发布  || 已完赛  已发布-->
        <el-button
          v-if="row.status == 1 && isExamStatus(row) >= 2"
          type="text"
          @click="cancelPublishExam(row.examId)"
          >撤销发布</el-button
        >
        <!-- -------------------------- -->
        <!-- 未开赛  未发布 -->
        <el-button
          v-if="row.status == 0"
          type="text"
          @click="publishExam(row.examId)"
          >发布</el-button
        >
        <!-- 未开赛 未发布 -->
        <el-button
          v-if="row.status == 0"
          type="text"
          @click="onEdit(row.examId)"
          >编辑
        </el-button>
        <!-- 未开赛  未发布 -->
        <el-button
          v-if="row.status == 0"
          type="text"
          @click="onDelete(row.examId)"
          class="red"
          >删除
        </el-button>
        <!-- ---------------------- -->

        <!-- 已开赛，已发布 -->
        <el-button v-if="isExamStatus(row) == 1 && row.status == 1" type="text"
          >已开赛，不允许操作
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页区域 -->
  <el-pagination
    background
    size="small"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
    v-model:current-page="params.pageNum"
    v-model:page-size="params.pageSize"
    :page-sizes="[1, 3, 5, 10, 15, 20]"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  />
</template>

<script setup>
import {
  cancelPublishExamService,
  deleteExamService,
  getExamListService,
  publishExamService,
} from "@/apis/exam";
import router from "@/router";
import { Plus } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { reactive, ref } from "vue";

function isNotStartExam(exam) {
  const now = new Date(); //当前时间
  return new Date(exam.startTime) > now;
}

//判断完赛状态
function isExamStatus(exam) {
  const now = new Date(); //当前时间
  if (new Date(exam.startTime) < now && new Date(exam.endTime) > now) {
    return 1; //已开赛
  } else if (new Date(exam.startTime) > now) {
    return 2; //未开赛
  } else {
    return 3; //已完赛
  }
}

const params = reactive({
  pageNum: 1,
  pageSize: 10,
  startTime: "",
  endTime: "",
  title: "",
});

const examList = ref([]);
const total = ref(0);
const datetimeRange = ref([]);

async function getExamList() {
  //将时间选择器的数据赋值给params
  if (datetimeRange.value[0] instanceof Date) {
    params.startTime = datetimeRange.value[0].toISOString();
  }
  if (datetimeRange.value[1] instanceof Date) {
    params.endTime = datetimeRange.value[1].toISOString();
  }
  const result = await getExamListService(params);
  examList.value = result.rows;
  total.value = result.total;
  console.log(examList.value);
  console.log(result);
}
getExamList(); //获取竞赛列表数据

//切换每页的大小
async function handleSizeChange(newSize) {
  // params.pageSize = newSize;
  params.pageNum = 1;
  getExamList();
}

//切换页码
async function handleCurrentChange(newPage) {
  // params.pageNum = newPage;
  getExamList();
}

//点击搜索
async function onSearch() {
  params.pageNum = 1;
  getExamList(params);
}

//重置搜索条件
async function onReset() {
  params.title = "";
  params.startTime = "";
  params.endTime = "";
  params.pageNum = 1;
  params.pageSize = 10;
  datetimeRange.value.length = 0;
  getExamList();
}

//添加竞赛，跳转新页面
async function onAddExam() {
  router.push("/layout/updateExam?type=add");
}

//编辑竞赛
async function onEdit(examId) {
  router.push(`/layout/updateExam?type=edit&examId=${examId}`);
}

//删除竞赛
async function onDelete(examId) {
  await deleteExamService(examId);
  params.pageNum = 1;
  getExamList();
}
//发布竞赛
async function publishExam(examId) {
  await publishExamService(examId)
  ElMessage.success("发布成功")
  getExamList()
}
//撤销发布
async function cancelPublishExam(examId) {
  await cancelPublishExamService(examId)
  ElMessage.success("撤销成功")
  getExamList()
}




</script>
